package com.idea.cery.springboot1.controller;

import com.idea.cery.springboot1.bean.User;
import com.idea.cery.springboot1.service.JdbcService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author cery
 * @date 2020/3/12
 */
@Api(description = "Jdbc 数据库操作")
//@Controller //需要每个方法指定返回类型
@RestController     //全部返回json(包括对象)
@EnableAutoConfiguration
@RequestMapping("/jc")
public class JdbcController {
    @Autowired
    JdbcService jdbcService;

    /**
     * 测试jdbc使用
     *
     * @return
     */
    @RequestMapping(value = "/jdbcFind", method = RequestMethod.GET)
    public String jdbcFind() {
        List<Map<String, Object>> lm = jdbcService.findAllUser();

        return lm.toString();
    }

    @RequestMapping(value = "/jdbcIn", method = RequestMethod.GET)
    public String jdbcInsert() {
        User user = new User();
        user.setId("5");
        user.setName("hsh");
        user.setAge(22);
        user.setSex("1");

        return jdbcService.insertUser(user);
    }

    @RequestMapping(value = "/jdbcUpdate/{age}/{sex}", method = RequestMethod.GET)//访问路径：/window/jdbcUpdate/18/1
    public String jdbcUpdate(@PathVariable int age, @PathVariable String sex) {
        User user = new User();
        user.setId("5");
        user.setName("hsh");
        user.setAge(age);
        user.setSex(sex);

        return jdbcService.update(user);
    }

    @RequestMapping(value = "/jdbcDel", method = RequestMethod.GET) //访问路径：/window/jdbcDel?id=5
    public String jdbcDelete(@RequestParam("id") String id) {
        return jdbcService.deleteById(id);
    }

}
